package com.shj.em.base.audit;

import com.baomidou.mybatisplus.entity.DefaultMetaObjectHandler;
import com.shj.em.util.ShiroUtils;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

public class AuditMetaObjectHandler extends DefaultMetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Date date = new Date();
        setFieldValByName("createBy", ShiroUtils.getUserId(), metaObject);
        setFieldValByName("createDt", date, metaObject);
        setFieldValByName("updateBy", ShiroUtils.getUserId(), metaObject);
        setFieldValByName("updateDt", date, metaObject);
        setFieldValByName("version",1L , metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName("updateBy", ShiroUtils.getUserId(), metaObject);
        setFieldValByName("updateDt", new Date(), metaObject);
    }

    @Override
    public boolean openInsertFill() {
        return true;
    }

    @Override
    public boolean openUpdateFill() {
        return true;
    }
}
